﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace myking
{
    public partial class FrmTwo : Form
    {
        public FrmTwo()
        {
            InitializeComponent();
        }

        KeyboardHook k_hook;

        public static string scriptpathname { get; set; }

        private void FrmTwo_Load(object sender, EventArgs e)
        {
            k_hook = new KeyboardHook();
            k_hook.KeyDownEvent += new KeyEventHandler(hook_KeyDown);//钩住键按下 
            k_hook.Start();//安装键盘钩子

            scriptpathname = Properties.Settings.Default.scriptpath;
            label1.Text = scriptpathname;

        }

        private void hook_KeyDown(object sender, KeyEventArgs e)
        {
            //判断按下的键（Alt + A） 
            //if (e.KeyValue == (int)Keys.A && (int)Control.ModifierKeys == (int)Keys.Alt) 
            if (e.KeyValue == (int)Keys.F7)
            {
                //Log.WriteLog("debug", "开始", "");
                RunScript();
                //System.Windows.Forms.MessageBox.Show("按下了开始"); 
            }
            if (e.KeyValue == (int)Keys.F8)
            {
                //Log.WriteLog("debug", "结束", "");
                StopScript();
                //System.Windows.Forms.MessageBox.Show("按下了结束"); 
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                scriptpathname = openFileDialog1.FileName;
                label1.Text = scriptpathname;

                //Properties.Settings.Default.scriptpath = scriptpathname;
                Properties.Settings.Default.Save();
            }
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            RunScript();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            StopScript();
        }

        #region 业务调用

       
        private void loopsendmessage()
        {
            scriptClass.MySendMessage();
        }

        private void RunScript()
        {
            string errmsg = string.Empty;
            ArrayList commandlist = new ArrayList();
            if (scriptClass.GetScript(scriptpathname, out errmsg) < 0)
            {
                MessageBox.Show(errmsg);
                return;
            }

            int delay = Convert.ToInt32(txtsec.Text);
            Thread.Sleep(delay);

            scriptClass.programhWnd = this.Handle;
            scriptClass.dmtypeFlag = "Y";
            scriptClass.businesstypeName = "Y";
            scriptClass.reconnetFlag = "N";
            scriptClass.frontType = "Y";
            Thread f = new Thread(loopsendmessage);
            f.Start();
            button7.Enabled = false;
            btnRun.Enabled = false;
            btnStop.Enabled = true;
        }

        private  void StopScript()
        {
            scriptClass.businesstypeName = "N";
            scriptClass.dmDispose();
            button7.Enabled = true;
            btnRun.Enabled = true;
            btnStop.Enabled = false;
        }

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            ScriptRun.Keydown("shift", 1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ScriptRun.Keydown("shift", 2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            name = "Don't Starve Together";
            IntPtr hwndAfter = scriptClass.FindWindowEx((IntPtr)0, (IntPtr)0, null, name);
            scriptClass.setwindowHide(hwndAfter);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            IntPtr hwndAfter = scriptClass.FindWindowEx((IntPtr)0, (IntPtr)0, null, name);
            scriptClass.setwindowShow(hwndAfter);
            
        }
    }
}
